package cn.zjnu.study.controller;

import cn.zjnu.study.bean.Msg;
import cn.zjnu.study.bean.User;
import cn.zjnu.study.bean.Video;
import cn.zjnu.study.bean.VideoX;
import cn.zjnu.study.service.UserService;
import cn.zjnu.study.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private UserService userService;

    @GetMapping("/videos/{uid}")
    public Msg getVideos(@PathVariable("uid") int userId,
                         @RequestParam(value = "pn", defaultValue = "1") int pn) {
        PageHelper.startPage(pn, 9);
        User user = userService.getUserById(userId);
        user.setPassword(null);
        List<Video> videos = videoService.getVideosByUserId(userId);
        PageInfo<Video> pageInfo = new PageInfo<>(videos, 5);
        return Msg.success().addExtra("pageInfo", pageInfo).addExtra("user", user);

    }

    @GetMapping("/videos")
    public Msg getAllVideos(@RequestParam(value = "pn", defaultValue = "1") int pn) {
        PageHelper.startPage(pn, 16);
        List<Video> videos = videoService.getAllVideos();
        List<VideoX> videoXES = new ArrayList<>();
        for (Video video : videos) {
            User user = userService.getUserById(video.getUserId());
            VideoX videoX = new VideoX(user, video);
            videoXES.add(videoX);
        }
        PageInfo<VideoX> pageInfo = new PageInfo<>(videoXES, 8);
        return Msg.success().addExtra("pageInfo", pageInfo);
    }

    @PostMapping("/uploadVideo")
    public Msg uploadVideo(@RequestParam("file") MultipartFile file,
                           @RequestParam(value = "cover", required = false) MultipartFile cover,
                           @RequestParam("videoName") String videoName,
                           HttpSession session) {
        User user = (User) session.getAttribute("user");
        String realPath = "";
        try {
            realPath = videoService.uploadVideo(file, user, cover, videoName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Msg.success(realPath);
    }

    @PostMapping("/video")
    public Msg insertVideo(HttpSession session,
                           @RequestParam("videoName") String videoName,
                           @RequestParam("path") String path) {
        User user = (User) session.getAttribute("user");
        int userId = user.getUserId();
        Video video = new Video();
        video.setCover(null);
        video.setPath(path);
        video.setTypeId(1);
        video.setUploadTime(new Date());
        video.setVideoName(videoName);
        video.setUserId(userId);
        videoService.insertVideo(video);
        return Msg.success();
    }

}
